﻿using UnityEngine;


namespace UFrame
{
    public class SpriteAlphaGroup : MonoBehaviour
    {
        public SpriteRenderer[] mGroup;
        public float mTimer = 0f;
        private float mLerpTime = 0f;

        private bool mIsPlayerAnima;


        public Color mStartColor = Color.white;
        public Color mEndColor = Color.red;

        private Color mAC;
        private Color mEC;

        [SerializeField] public bool mIsTestPlayerAnima;


        // Use this for initialization
        void Start()
        {
            mAC = mStartColor;
            mEC = mEndColor;

            mLerpTime = mTimer;
        }

        void Update()
        {
            if (mIsPlayerAnima)
            {
                mTimer -= Time.deltaTime;
                float lerp = Mathf.PingPong(mTimer, mLerpTime) / mLerpTime;
                Color newC = Color.Lerp(mEC, mAC, lerp);
                SetColor(newC);
                if (mTimer <= 0)
                {
                    mIsPlayerAnima = false;
                    mTimer = 0f;
                    SetColor(mEC);
                }
            }

            if (mIsTestPlayerAnima)
            {
                StartPlay();
                mIsTestPlayerAnima = false;
            }
        }

        public void StartPlay()
        {
            mIsPlayerAnima = true;
        }

        public void SetColor(Color color)
        {
            if (mGroup == null)
            {
                return;
            }

            for (int i = 0; i < mGroup.Length; i++)
            {
                mGroup[i].color = color;
            }
        }
    }
}